<?php

namespace App\Models\Receivemoney;

use App\Models\Manger\Manger;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;

class Receivemoney extends Model
{
    protected $table = 'sale_receive_money';
    public $timestamps = false;
    protected $primaryKey = 'id';
    protected $guarded = ['_token'];

    /**
     * 列表
     */
    public static function getList(Request $request, $mangerinfo, $isgetall)
    {
        $cpid = $mangerinfo['cpid'] ?? 0;
        $sys_dpm_id = $mangerinfo['sys_dpm_id'] ?? 0;
        $query = self::with('manger')
            ->where('is_del', config('constants.is_del.no'))
            ->orderBy('create_at', 'desc');

        //公司名称
        $firm_name = $request->firm_name;
        if ($firm_name != '') {
            $query->where('firm_name', 'like', '%' . $firm_name . '%');
        }

        //账户名称
        $account_name = $request->account_name;
        if ($account_name != '') {
            $query->where('settlement_company', 'like', '%' . $account_name . '%');
            $query->orWhere('bank_name', 'like', '%' . $account_name . '%');
        }

        $receiver_at_start_time = $request->receiver_at_start_time;
        if ($receiver_at_start_time != '') {
            $query->where('receiver_at', '>=', $receiver_at_start_time);
        }
        $receiver_at_end_time = $request->receiver_at_end_time;
        if ($receiver_at_end_time != '') {
            $query->where('receiver_at', '<=', $receiver_at_end_time);
        }

        $start_time = $request->start_time;
        if ($start_time != '') {
            $query->where('date', '>=', $start_time);
        }

        $end_time = $request->end_time;
        if ($end_time != '') {
            $query->where('date', '<=', $end_time);
        }

        //金额
        $amount = $request->amount;
        if ($amount != '') {
            $amount = mathfuncmoney('mul', $amount, 100);
            $query->where('amount', $amount);
        }

        //待认领 1 已认领3
        $status = $request->status;
        if ($status > 0) {
            if ($status == 1) {
                $query->whereIn('status', [1, 2]);
            } else {
                $query->where('status', $status);
            }
        }

        if ($cpid > 0) {
            $query->where('cpid', $cpid);
        }

        // if ($sys_dpm_id > 0) {
        //     $query->where('sys_dpm_id', $sys_dpm_id);
        // }

        //$isallfield 1所有字段 2特定字段
        $is_all_field = $request->is_all_field;
        if ($is_all_field == 2) {
            $query->select(['id', 'amount', 'date', 'settlement_company', 'bank_name', 'status']);
        }

        if ($isgetall == 0) {
            $limit = $request->limit ?? 10;
            $list = $query->paginate($limit)->toArray();
            return paginateDataToArray($list);
        } else {
            $list = $query->get()->toArray();
            return ['list' => $list];
        }
    }

    /**
     * 详情
     */
    public static function getone($id)
    {
        return self::with('manger')->where('id', $id)->first();
    }

    /**
     * 关联认领者
     */
    public function manger()
    {
        return $this->hasOne(Manger::class, 'id', 'receiver_id');
    }
}
